package processs;

import java.io.IOException;
import java.util.ArrayList;

import models.Lib2;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import utility.Parser;
import utility.StreamUtils;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

public class Getyou2 implements Runnable{
	
	public static final String Listvid = "Listvid";
	private final Handler repto;
	public static ArrayList<models.Listvid> al;
	private int total;
	String id;
	String pdate;
	String ti;
	String thumb;
	int cat_id;
	Parser ps;
	
	public Getyou2(Handler repto1){
		this.repto=repto1;
	}
	
	public void run() {
		// TODO Auto-generated method stub
		ps=new Parser();
		al=new ArrayList<models.Listvid>();
		JSONObject jo=ps.getjson("https://gdata.youtube.com/feeds/api/users/VietNamGameTV/playlists?v=2&alt=jsonc&max-results=50");
		try{
	//		hc=new DefaultHttpClient();
	//		hr=new HttpGet("https://gdata.youtube.com/feeds/api/users/VietNamGameTV/playlists?v=2&alt=jsonc&max-results=50");
		//	hpon=hc.execute(hr);
	//		js=StreamUtils.convertToString(hpon.getEntity().getContent());
		//	JSONObject jo=new JSONObject(js);
			JSONArray ja=jo.getJSONObject("data").getJSONArray("items");
			String str=jo.getJSONObject("data").getString("totalItems");
			total=Integer.parseInt(str);
			Log.d("RealSIze", ""+total);
			
			for(int i=0;i<ja.length();i++){
				JSONObject jsonObject = ja.getJSONObject(i);
				id=jsonObject.getString("id");
				pdate=jsonObject.getString("created");
				ti=jsonObject.getString("title");
				thumb=jsonObject.getJSONObject("thumbnail").getString("sqDefault");
				if(ti.contains("AOE")){
					cat_id=1;
				}else if(ti.contains("PET")){
					cat_id=2;
				}else if(ti.contains("LMHT")){
					cat_id=3;
				}else if(ti.contains("CS 1.1")||ti.contains("Counter Strike")){
					cat_id=4;
				}else if(ti.contains("3Q")){
					cat_id=5;
				}else if(ti.contains("SC2")){
					cat_id=6;
				}
				else{
					cat_id=7;
				}
				al.add(new models.Listvid(id, ti, pdate, thumb, cat_id));
				
			}
			
		}catch(JSONException ef){
			Log.e("hel", ef.getMessage());
		}
		
		try{
		for(int j=51;j<total;j+=50){
			String url="https://gdata.youtube.com/feeds/api/users/VietNamGameTV/playlists?v=2&alt=jsonc&max-results=50&start-index="+j+"";
	//		hc=new DefaultHttpClient();
		//	hr=new HttpGet(url);
	//		hpon=hc.execute(hr);
		//	js=StreamUtils.convertToString(hpon.getEntity().getContent());
		//	JSONObject jo=new JSONObject(js);
			JSONObject jso=ps.getjson(url);
			JSONArray ja=jso.getJSONObject("data").getJSONArray("items");
			for(int i=0;i<ja.length();i++){
				JSONObject jsonObject = ja.getJSONObject(i);
				id=jsonObject.getString("id");
				pdate=jsonObject.getString("created");
				ti=jsonObject.getString("title");
				thumb=jsonObject.getJSONObject("thumbnail").getString("sqDefault");
				if(ti.contains("AOE")){
					cat_id=1;
				}else if(ti.contains("PET")){
					cat_id=2;
				}else if(ti.contains("LMHT")){
					cat_id=3;
				}else if(ti.contains("CS 1.1")||ti.contains("Counter Strike")){
					cat_id=4;
				}else if(ti.contains("3Q")){
					cat_id=5;
				}else if(ti.contains("SC2")){
					cat_id=6;
				}
				else{
					cat_id=7;
				}
				al.add(new models.Listvid(id, ti, pdate, thumb, cat_id));
				Log.d("Allsize", ""+al.size());
			}
			
		}
		
		}catch(JSONException ef){
			Log.e("hel", ef.getMessage());
		}
		Lib2 li=new Lib2(al);
		Bundle bd=new Bundle();
		bd.putSerializable(Listvid, li);
		Message msg=Message.obtain();
		msg.setData(bd);
		repto.sendMessage(msg);
	}

}
